﻿/*
   Command line argument
   Created by Randall Maas, Saturday October 17 2009
   Copyright (c) 2009, Randall Maas
   All rights reserved.

   Redistribution and use in source and binary forms, with or without
   modification, are permitted provided that the following conditions are met:

   Redistributions of source code must retain the above copyright notice, this
   list of conditions and the following disclaimer.

   Redistributions in binary form must reproduce the above copyright notice,
   this list of conditions and the following disclaimer in the documentation
   and/or other materials provided with the distribution.

   Neither the name of Blackwood Designs, LLC nor the names of its contributors
   may be used to endorse or promote products derived from this software without
   specific prior written permission.

   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
   AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
   IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
   ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
   LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
   CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
   SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
   INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 
   CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
   ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
   THE POSSIBILITY OF SUCH DAMAGE.
*/

using System ;
using System . Collections . Generic;
using System . IO;
using System . Windows . Forms;
using System . Globalization;

static partial class Program
{
   /// <summary>
   /// Processes the command line arguments
   /// </summary>
   /// <param name="Args">List of command line paramters</param>
   static void CmdLine(string[] Args)
   {
      int Idx=0;
      flagAction Action;
      try
      {
         // Iterate over the arguments and gather information
         for (; Idx < Args . Length && 0 == Shutdown; Idx++)
          if (FlagAction . TryGetValue(Args[Idx] . ToUpper(), out Action))
            Action(Args, ref Idx);
           else
            {
               // Punt to displaying output
               Help(Args, ref Idx);
            }
      }
      catch
      {
         Help(Args, ref Idx);
      }
   }

   /// <summary>
   /// Delegate used in the command line processing
   /// </summary>
   /// <param name="Args">The command line params</param>
   /// <param name="Idx">The current index into the params</param>
   delegate void flagAction(string[] Args, ref int Idx);

   /// <summary>
   /// A table that looks up the flag and the handler
   /// </summary>
   static Dictionary<string, flagAction> FlagAction = new Dictionary<string, flagAction>();
   static Program()
   {
      // These keys need to be in upper case
      FlagAction["/?" ] = Help;
      FlagAction["-H" ] = Help;
      FlagAction["-HELP" ] = Help;
      FlagAction["--HELP"] = Help;
      FlagAction["-NAME" ] = NameParam;
      FlagAction["-ID"   ] = IdParam;
      FlagAction["-AGGRESSIVE"]=AggressiveParam;
   }

   /// <summary>
   /// Specifies the name of the application or window to look for
   /// </summary>
   /// <param name="Args">The command line params</param>
   /// <param name="Idx">The current index into the params</param>
   static void NameParam(string[] Args, ref int Idx)
   {
      AppName = Args[++Idx] . Trim();
   }


   static void AggressiveParam(string[] Args, ref int Idx)
   {
      Aggressive = true;
      IdleTimeTrigger = 5000;
      IdleTimeTrigger = 60*1000;
      Probs        = Hard;
      AudioThresh  = 0.4;
      AudioThresh2 = 0.05;
      ShakeCoef    = 4;
      AngleCoef    = 0.2;
   }

   /// <summary>
   /// Specifies the process id or the window handle to look for
   /// </summary>
   /// <param name="Args">The command line params</param>
   /// <param name="Idx">The current index into the params</param>
   static void IdParam(string[] Args, ref int Idx)
   {
      int Result;
      bool B;
      string S = Args[++Idx] . Trim();
      if (S . Length > 2 && "0X" == S . Substring(0,2) . ToUpper())
        B = int . TryParse(S . Substring(2), NumberStyles . HexNumber, CultureInfo.CurrentCulture, out Result);
       else
        B = int . TryParse(S, out Result);
      if (!B)
        Help(Args, ref Idx);
       else
        AppId = (IntPtr) Result;
   }


   /// <summary>
   /// Displays the banner of the command line parameters
   /// </summary>
   /// <param name="Args">The command line params</param>
   /// <param name="Idx">The current index into the params</param>
   static void Help(string[] Args, ref int Idx)
   {
      Console . WriteLine("A sneaky application that fiddles with the windows");
      Console . WriteLine("gremlin [http://gremlin.codeplex.com/] (c) 2009 Randall Maas ");
      Console . WriteLine("");
      Console . WriteLine("/?            Prints this message");
      Console . WriteLine("--help        Prints this message");
      Console . WriteLine("-h            Prints this message");
      Console . WriteLine("-id           Process id, or window handle [maybe 0xhex or decimal]");
      Console . WriteLine("-name         Name of window or application");
      Console . WriteLine("-aggressive   Uses a more aggressive attention getting strategy");
      Shutdown = 1;
      Application . Exit();
   }


}
